package com.foreknow.elm.service.Impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.Impl.CartDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
    @Override
    public List<Cart> listCart(String userId, Integer businessId) {
        CartDao cd=new CartDaoImpl();
        List<Cart> list=new ArrayList<>();
        try {
            list=cd.listCart(userId,businessId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) {
        int i=0;
        CartDao cd=new CartDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=cd.saveCart(userId,businessId,foodId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) {
        int i=0;
        CartDao cd=new CartDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=cd.updateCart(userId,businessId,foodId,quantity);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) {
        int i=0;
        CartDao cd=new CartDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=cd.removeCart(userId,businessId,foodId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    public static void main(String[] args) {
        CartService cs=new CartServiceImpl();

//        List<Cart> list = cs.listCart("11111111111", 0);
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getBusiness()==null){
//                System.out.println(list.get(i).getCartId() + "---" + list.get(i).getUserId() + "---" + list.get(i).getFood().getFoodId() + "---" + list.get(i).getFood().getFoodName()+list.get(i).getBusinessId());
//            }else{
//                System.out.println(list.get(i).getCartId() + "---" + list.get(i).getUserId() + "---" + list.get(i).getBusinessId() + list.get(i).getBusiness().getBusinessName()+ "---" + list.get(i).getFood().getFoodId() + "---" + list.get(i).getFood().getFoodName());
//            }
//        }

//        int i=cs.saveCart("12345671111",10002,3);
//        System.out.println(i);

        System.out.println(cs.updateCart("11111111111",10002,4,80));

//          System.out.println(cs.removeCart("12345671111",10002,5));
    }
    }

